#include "head.h"

#define TCP_SERVER_IP_STR "192.168.31.13"
#define TCP_SERVER_PORT   50000
#define TCP_SERVER_MAX_LISTEN 100

/**************************************************************************
 * 函数名称: TcpServerInitServer
 * 函数说明: 初始化 TCP Server, 
 *          创建并命名 TCP Server Socket (socktet, bind)
 *          创建套接字队列 (listen)
 * 参   数： strServerIP : 待创建的 TCP Server IP
 *          ServerPort  : 待创建的 TCP Server 端口
 * 返回值  : TCP Server Socket ( >0 整数 )
 *          -1 初始化失败 
 *************************************************************************/
int TcpServerInitServer( const char strServerIP[], const int ServerPort )
{
    int TcpServerSockFd = socket( AF_INET, SOCK_STREAM, 0 );
    if ( -1 == TcpServerSockFd )
    {
        perror( "fail socket @ TcpServerInitServer" );
        return -1;
    }

    struct sockaddr_in TcpServerAddr;
    TcpServerAddr.sin_family = AF_INET;
    TcpServerAddr.sin_port   = ServerPort;
    TcpServerAddr.sin_addr.s_addr = inet_addr( strServerIP );

    int Ret = bind( TcpServerSockFd, (struct sockaddr *)&TcpServerAddr, sizeof(TcpServerAddr) );
    if ( -1 == Ret )
    {
        perror( "fail bind @ TcpServerInitServer" );
        return -1;
    }

    Ret = listen( TcpServerSockFd, TCP_SERVER_MAX_LISTEN );
    if ( -1 == Ret )
    {
        perror( "fail listen @ TcpServerInitServer" );
        return -1;
    }

    return TcpServerSockFd;

}

int main(int argc, char const *argv[])
{
    int TcpServerSockFd = TcpServerInitServer( TCP_SERVER_IP_STR, TCP_SERVER_PORT );


    return 0;
}
